/*
 * JaCoLiB
 * Copyright (C) 2008 Alessandro Serra
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package it.gashale.jacolib.main.configurator.scripts;

import it.gashale.jacolib.core.JacolibError;
import it.gashale.jacolib.main.Jacolib;
import it.gashale.jacolib.main.configurator.Configurator;
import it.gashale.jacolib.main.configurator.Environment;

import java.util.StringTokenizer;

public class JacolibConfigurator extends Configurator {
	public JacolibConfigurator(Environment env) {
		super(env);
	}
	public void configure(Jacolib jacolib) throws JacolibError {
		configure(jacolib,null);
	}
	public void configure(Jacolib jacolib, String conf) throws JacolibError {
		String confs=getValue(new String[] {"InitialConfigurators"},null);
		StringTokenizer st = new StringTokenizer(confs);
		while (st.hasMoreTokens()) {
			String full_prefix=st.nextToken();
			String[] prefixes=full_prefix.split("\\.");
			
			if(conf==null) {
				configure_p(jacolib,prefixes,true);
			} else if(conf.equals(full_prefix)) {
				configure_p(jacolib,prefixes,false);
			}
		}
	}
	protected void configure_p(Jacolib jacolib, String[] prefixes, boolean auto) throws JacolibError {
		setPrefixes(prefixes);
		String configurator_class=getValue(new String[]{"Configurator"}, null);
		String autoload=getValue(new String[]{"Autoload"}, "false");
		if(auto && autoload.equals("false")) return;
		if(configurator_class!=null) {
			Configurator configurator=newConfigurator(configurator_class, getEnvironment());
			configurator.setPrefixes(getPrefixes());
			configurator.configure(jacolib);
		} else {
			throw new JacolibError("Undefine Configurator.");
		}
	}
}
